//
//  RentListViewController.m
//  ZhuanZuBao
//
//  Created by bchy on 2020/8/28.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "RentCell.h"
#import "RentDetailViewController.h"
#import "RentModel.h"

@interface MyCollectionViewController ()

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation MyCollectionViewController

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的收藏";
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page ++;
        [self getData];
    }];
    
    _page = 1;
    [self getData];
    [MBProgressHUD showMessag:@"" toView:self.view];
}

//city     否     string     城市
//adr     否     string     区县
//xq     否     string     小区
//tags     否     string     标签
-(void)getData{
    NSDictionary *dict = @{
        @"uid": [User sharedUser].userId,
        @"token": [User sharedUser].token,
        @"pge":@(_page),
        @"cnt":@"10",
    };
    [NetWork sendGetUrl:@"mem/my_fav.php" withParams:dict success:^(id responseBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@", responseBody);
        NSArray *arr = [RentModel mj_objectArrayWithKeyValuesArray:responseBody[@"lst"]];
        
        if (self.page == 1) {
            [self.dataArr removeAllObjects];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_header endRefreshing];
            if (arr.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
        }
        
        [self.dataArr addObjectsFromArray:arr];
        [self.tableView reloadData];
        if (self.dataArr.count == 0) {
            [self addEmptyView];
        }else{
            [self removeEmptyView];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RentModel *rent = _dataArr[indexPath.row];
    RentCell *cell = [RentCell cellWithTableView:tableView];
    cell.rent = rent;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RentModel *rent = _dataArr[indexPath.row];
    RentDetailViewController *vc = [[RentDetailViewController alloc] initWithRentId:rent.rentId];
    [self.navigationController pushViewController:vc animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    [MBProgressHUD showMessag:@"" toView:self.view];
    RentModel *rent = _dataArr[indexPath.row];
    NSDictionary *dict = @{
                           @"id":rent.rentId,
                           @"uid": [User sharedUser].userId,
                           @"token": [User sharedUser].token
                           };
    [NetWork sendGetUrl:@"fy/fy_del.php" withParams:dict success:^(id responseBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.dataArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        if (self.dataArr.count == 0) {
            [self addEmptyView];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

@end
